package com.car.carsharing.controller;

import com.car.carsharing.dto.CouponDTO;
import com.car.carsharing.dto.ResponseDTO;
import com.car.carsharing.dto.TableListDto;
import com.car.carsharing.mapper.UserCouponMapper;
import com.car.carsharing.pojo.Coupon;
import com.car.carsharing.service.CouponService;
import com.car.carsharing.service.UserCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class CouponController {
    @Autowired
    private CouponService couponService;
    @Autowired
    private UserCouponService userCouponService;
    @RequestMapping("getCoupon")
    public TableListDto getCoupon(@RequestBody Map map){

        return  couponService.getCoupon(map);
    }

    @RequestMapping("buyCoupon")
    public ResponseDTO buyCoupon(@RequestBody Map map){
        Integer userId = (Integer) map.get("userId");
        Integer couponId = (Integer) map.get("couponId");
        Integer r = userCouponService.buyCoupon(userId,couponId);
        if(r== -1){
            return new ResponseDTO().success("已购买，不可重复购买");
        }
        return new ResponseDTO().success("购买成功");
    }

    @RequestMapping("myCoupon")
    public ResponseDTO myCoupon(@RequestBody Map map){
        List<CouponDTO> coupons = couponService.getUserCouponById((Integer) map.get("userId"));
        return new ResponseDTO().success(coupons,"获得成功");
    }

    @RequestMapping("updateCoupon")
    public ResponseDTO updateCoupon(@RequestBody Coupon coupon){
       int r  = couponService.updateCoupon(coupon);
        return new ResponseDTO().success("修改成功");
    }

    @RequestMapping("addCoupon")
    public ResponseDTO addCoupon(@RequestBody Coupon coupon){
        int r = couponService.addCoupon(coupon);
        return new ResponseDTO().success("新增成功");
    }

    @RequestMapping("getCouponNum")
    public ResponseDTO getCouponNum(){
        List<CouponDTO> couponDTOS = couponService.getCouponNum();
        return new ResponseDTO().success(couponDTOS,"获取成功");
    }

}
